home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / quicktime / quicktime for java / zoo tutorial / module 6- advanced compositing / source / animalpane.java next >
Encoding:
Java Source  |  2000-06-23  |  12.5 KB  |  422 lines

  1. import java.awt.Dimension;
  2. import java.io.IOException;
  3. import java.io.FileNotFoundException;
  4.  
  5. import quicktime.app.QTFactory;
  6. import quicktime.app.anim.Compositor;
  7. import quicktime.app.event.QTActionEvent;  
  8. import quicktime.app.event.QTActionListener;
  9. import quicktime.app.event.QTMouseTargetController; 
  10. import quicktime.app.image.GraphicsImporterDrawer; 
  11. import quicktime.app.image.ImagePresenter; 
  12. import quicktime.app.image.ImageUtil;
  13. import quicktime.app.ui.ReleaseButton;
  14. import quicktime.app.ui.ButtonActivator;
  15. import quicktime.app.players.MoviePresenter;
  16. import quicktime.app.players.QTPlayer;
  17.  
  18. import quicktime.io.QTFile;
  19. import quicktime.io.OpenMovieFile;
  20.  
  21. import quicktime.qd.QDRect;
  22. import quicktime.qd.QDGraphics;
  23. import quicktime.qd.QDColor; 
  24. import quicktime.qd.QDConstants; 
  25.  
  26. import quicktime.std.StdQTConstants;
  27. import quicktime.std.movies.Movie; 
  28. import quicktime.std.image.Matrix;
  29.  
  30. import quicktime.QTSession;
  31. import quicktime.QTException; 
  32.  
  33. /**
  34.  * Module 5 - Custom Movie Controllers
  35.  * This application requires QuickTime for Java
  36.  *
  37.  * @author Levi Brown
  38.  * @author Michael Hopkins
  39.  * @author Apple Computer, Inc.
  40.  * @version 4.1 11/16/1999
  41.  *
  42.  * Copyright:     © Copyright 1999 Apple Computer, Inc. All rights reserved.
  43.  *    
  44.  * Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  45.  *                ("Apple") in consideration of your agreement to the following terms, and your
  46.  *                use, installation, modification or redistribution of this Apple software
  47.  *                constitutes acceptance of these terms.  If you do not agree with these terms,
  48.  *                please do not use, install, modify or redistribute this Apple software.
  49.  *
  50.  *                In consideration of your agreement to abide by the following terms, and subject
  51.  *                to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  52.  *                copyrights in this original Apple software (the "Apple Software"), to use,
  53.  *                reproduce, modify and redistribute the Apple Software, with or without
  54.  *                modifications, in source and/or binary forms; provided that if you redistribute
  55.  *                the Apple Software in its entirety and without modifications, you must retain
  56.  *                this notice and the following text and disclaimers in all such redistributions of
  57.  *                the Apple Software.  Neither the name, trademarks, service marks or logos of
  58.  *                Apple Computer, Inc. may be used to endorse or promote products derived from the
  59.  *                Apple Software without specific prior written permission from Apple.  Except as
  60.  *                expressly stated in this notice, no other rights or licenses, express or implied,
  61.  *                are granted by Apple herein, including but not limited to any patent rights that
  62.  *                may be infringed by your derivative works or by other works in which the Apple
  63.  *                Software may be incorporated.
  64.  *
  65.  *                The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  66.  *                WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  67.  *                WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  68.  *                PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  69.  *                COMBINATION WITH YOUR PRODUCTS.
  70.  *
  71.  *                IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  72.  *                CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  73.  *                GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  74.  *                ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  75.  *                OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  76.  *                (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  77.  *                ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  78.  * 
  79.  */
  80.  
  81. public class AnimalPane extends ZooPane
  82. {
  83.     /**
  84.      *  Public default constructor
  85.      *  Creates the map button, and sets up the listeners
  86.      */
  87.     public AnimalPane()
  88.     {
  89.         QDRect size = new QDRect( Zoo5.WIDTH, Zoo5.HEIGHT );
  90.         try
  91.         {
  92.             loadSound( "data/zebra/Zebra.au" );
  93.  
  94.             QDGraphics gw = new QDGraphics(size);
  95.             compositor = new Compositor( gw, QDColor.white, 30, 1 );
  96.                     
  97.             QTFile imageFile = new QTFile( QTFactory.findAbsolutePath( "data/zebra/ZebraBackground.jpg" ));
  98.             GraphicsImporterDrawer drawer = new GraphicsImporterDrawer( imageFile );
  99.             ImagePresenter presenter = ImagePresenter.fromGraphicsImporterDrawer(drawer);
  100.             presenter.setLocation( 110, 110 );
  101.             compositor.addMember( presenter, 3 );
  102.             
  103.             addText( "data/zebra/Zebra.txt", 2, 15, 160, 415, 220 );
  104.             
  105.             addMovie( "data/zebra/Zebra.mov", 1, 0, 0 );
  106.             
  107.             playSound();
  108.         }
  109.         catch ( IOException e )
  110.         {
  111.             e.printStackTrace();
  112.         }
  113.         catch ( QTException e )
  114.         {
  115.             e.printStackTrace();
  116.         }
  117.     }
  118.     
  119.     /**
  120.      * Listener object for the Movie controller buttons
  121.      * Called when the button is released
  122.      */
  123.     public class ButtonListener implements QTActionListener    
  124.     {
  125.         /**
  126.          * Called when the button is released
  127.          * @param ???
  128.          */
  129.         public void actionPerformed( QTActionEvent event ) 
  130.         {
  131.             try
  132.             {
  133.                 Object source = event.getSource();
  134.                 
  135.                 if (source.equals(playButton))            // Play >
  136.                 {
  137.                     if ( md.getRate() == 0 )
  138.                     {
  139.                         md.setRate( 1 );
  140.                     }
  141.                     stopButton.setCurrentImage( stopRel );
  142.                     stopButton.setReleasedImage( stopRel );
  143.                     playButton.setCurrentImage( playPlaying );
  144.                     playButton.setReleasedImage( playPlaying );
  145.                 }                
  146.  
  147.                 else if (source.equals(stopButton))        // Stop []
  148.                 {
  149.                     if ( md.getRate() == 1 )
  150.                     {
  151.                         md.setRate( 0 );
  152.                     }
  153.                     stopButton.setCurrentImage( stopDeactive );
  154.                     stopButton.setReleasedImage( stopDeactive );
  155.                     playButton.setCurrentImage( playRel );
  156.                     playButton.setReleasedImage( playRel );
  157.                 }
  158.                 else if (source.equals(rewindButton))    // Rewind <<
  159.                 {
  160.                     md.setTime(0);
  161.                 }
  162.             }
  163.             catch (QTException exc )
  164.             {
  165.                 exc.printStackTrace();
  166.             }
  167.         }
  168.     }
  169.     
  170.     /**
  171.      * Loads a graphics file from a relative path and creates an ImagePresenter object
  172.      * @param ???
  173.      */
  174.     public ImagePresenter makePresenterFromFile( String name )
  175.     {
  176.         try
  177.         {
  178.             QTFile file = new QTFile (QTFactory.findAbsolutePath (name));
  179.             GraphicsImporterDrawer drawer = new GraphicsImporterDrawer(file);
  180.             return ImagePresenter.fromGraphicsImporterDrawer(drawer);
  181.         }
  182.         catch (IOException e)
  183.         {
  184.             e.printStackTrace();
  185.         }
  186.         catch (QTException e)
  187.         {
  188.             e.printStackTrace();
  189.         }
  190.         return null;    
  191.     }
  192.     
  193.     /**
  194.      * Called to do any setup after being set as the client of the QTCanvas.
  195.      * May be used to start effects running, movies playing, etc.
  196.      */
  197.     public void start()
  198.     {
  199.         buttonController.addQTMouseListener(buttonActivator);
  200.         try
  201.         {
  202.             stopButton.setCurrentImage(  stopRel );         // Stop button should not have focus
  203.             playButton.setCurrentImage(  playPlaying );  // Play button should get focus (blue arrow)
  204.             playButton.setReleasedImage( playPlaying );
  205.         }
  206.         catch( QTException e )
  207.         {
  208.             e.printStackTrace();
  209.         }
  210.         try
  211.         {
  212.             md.setRate(1);                                // start the player
  213.         }
  214.         catch( QTException e )
  215.         {
  216.             e.printStackTrace();
  217.         }
  218.         compositor.getTimer().setRate(1);                // start the compositor
  219.     }
  220.     
  221.     /**
  222.      * Called to do clean up after being removed as the client of the QTCanvas.
  223.      * Should be used to stop effects running, movies playing, etc.
  224.      */
  225.     public void stop()
  226.     {
  227.         buttonController.removeQTMouseListener(buttonActivator);
  228.         compositor.getTimer().setRate(0);
  229.         try
  230.         {
  231.             md.setRate(0);
  232.         }
  233.         catch( QTException e )
  234.         {
  235.             e.printStackTrace();
  236.         }    
  237.     }
  238.  
  239.     /**
  240.      *  Plays the sound file associated with the current AnimalPane
  241.      */
  242.     public void playSound()
  243.     {
  244.         if( player == null )
  245.             return;
  246.  
  247.         try
  248.         {
  249.             player.setTime(0);            //Start the sound at the beginning
  250.             player.startTasking();         //Make sure the player gets time to play the sound
  251.             player.setRate(1);             //Start playing
  252.         }
  253.         catch( QTException e )
  254.         {
  255.             e.printStackTrace();
  256.         }
  257.     }
  258.  
  259.     /**
  260.      * Creates a Movie from the specified path and adds it
  261.      * to the compositor in the hardcoded location.
  262.      * @param moviePath the relative path to the movie file to add.
  263.      * @param layer, the layer of the compositor to add the movie to.
  264.      * @param x the x coordinate location.
  265.      * @param y the y coordinate location.
  266.      * @see #makeMovie
  267.      */
  268.     protected void addMovie(String moviePath, int layer, int x, int y)
  269.     {
  270.         try
  271.         {
  272.             Movie m = makeMovie (new QTFile (QTFactory.findAbsolutePath (moviePath)));
  273.             md = new MoviePresenter (m);
  274.  
  275.             md.setLocation (x, y);
  276.  
  277.             playRel     = makePresenterFromFile( "data/Play.jpg" );            // Play >
  278.             playPress   = makePresenterFromFile( "data/PlayPressed.jpg" );
  279.             playPlaying = makePresenterFromFile( "data/Playing.jpg" );
  280.  
  281.             playButton = new MovieButton( playRel, playPress, playPlaying );
  282.             playButton.setLabel ("Play");
  283.             playButton.setLocation( x + 68, y + 152 );
  284.             
  285.             stopRel      = makePresenterFromFile( "data/Stop.jpg" );        // Stop []
  286.             stopPress    = makePresenterFromFile( "data/StopPressed.jpg" );
  287.             stopDeactive = makePresenterFromFile( "data/Stopped.jpg" );
  288.  
  289.             stopButton  = new MovieButton( stopRel, stopPress, stopDeactive );
  290.             stopButton.setLabel ("Stop");
  291.             stopButton.setLocation( x + 136, y + 152 );
  292.             
  293.             rewRel      = makePresenterFromFile( "data/Rewind.jpg" );        // Rewind <<
  294.             rewPress    = makePresenterFromFile( "data/RewindPressed.jpg" );
  295.             rewDeactive = rewRel;
  296.         
  297.             rewindButton = new MovieButton( rewRel, rewPress, rewDeactive );
  298.             rewindButton.setLabel( "Rewind" );
  299.             rewindButton.setLocation( x, y + 152 );
  300.  
  301.             ButtonListener bl = new ButtonListener();                        // create a new listener for the buttons
  302.             
  303.             playButton.addActionListener(bl);
  304.             stopButton.addActionListener(bl);
  305.             rewindButton.addActionListener(bl);
  306.  
  307.             compositor.addMember(md, layer);                                // add the movie presenter            
  308.  
  309.             compositor.addMember(playButton);                                //     and buttons to the compositor
  310.             compositor.addMember(stopButton);
  311.             compositor.addMember(rewindButton);
  312.  
  313.             buttonController = new QTMouseTargetController (false);    
  314.             buttonController.addMember(playButton);
  315.             buttonController.addMember(stopButton);
  316.             buttonController.addMember(rewindButton);
  317.         
  318.             compositor.addController(buttonController);
  319.  
  320.             buttonActivator = new ButtonActivator();
  321.  
  322.             md.setRate(1);                                                    // start the movie        
  323.         }
  324.         catch (QTException exc)
  325.         {
  326.             exc.printStackTrace();
  327.         }
  328.         catch ( IOException exc )
  329.         {
  330.             exc.printStackTrace();
  331.         }    
  332.     }
  333.  
  334.     
  335.     /**
  336.      * Reads a text file from the disk and displays it in the compositor
  337.      * @param textPath the relative path where the text is located
  338.      * @param layer, the layer of the compositor to add the text to.
  339.      * @param x the x coordinate location.
  340.      * @param y the y coordinate location.
  341.      * @param width the width of the area to display the text.
  342.      * @param height the height of the area to display the text.
  343.      */
  344.     protected void addText(String textPath, int layer, int x, int y, int width, int height)
  345.     {
  346.         try
  347.         {
  348.             TextPresenter text = new TextPresenter(textPath, new Dimension(width, height));
  349.             
  350.             ImagePresenter presenter = text.getPresenter();
  351.             Matrix theMatrix = new Matrix();
  352.             theMatrix.translate( (float) x, (float) y );
  353.             presenter.setMatrix(theMatrix);
  354.             compositor.addMember(presenter, layer);
  355.         }
  356.         catch ( QTException exc )
  357.         {
  358.             exc.printStackTrace();
  359.         }
  360.         catch ( FileNotFoundException exc )
  361.         {
  362.             exc.printStackTrace();
  363.         }    
  364.     }
  365.     
  366.     /**
  367.      * Loads the specified sound file from disk and prepares it for playing
  368.      */
  369.     protected void loadSound( String soundPath )
  370.     {
  371.         try
  372.         {
  373.             String soundLocation = QTFactory.findAbsolutePath( soundPath ).getPath();
  374.             //this call works with a file://, http://, rtsp://located movie
  375.             player = (QTPlayer)QTFactory.makeDrawable ("file://" + soundLocation);
  376.         }
  377.         catch( IOException e )
  378.         {
  379.             e.printStackTrace();
  380.         }
  381.         catch( QTException e )
  382.         {
  383.             e.printStackTrace();
  384.         }
  385.     }
  386.         
  387.     /**
  388.      * Opens the movie file and sets it up to be played.
  389.      * @param f a QTFile representing the movie to initialize.
  390.      * @return the movie, ready to play
  391.      */
  392.     protected Movie makeMovie( QTFile f ) throws IOException, QTException
  393.     {
  394.         OpenMovieFile movieFile = OpenMovieFile.asRead(f);
  395.         Movie m = Movie.fromFile( movieFile );
  396.         m.getTimeBase().setFlags( StdQTConstants.loopTimeBase );    
  397.         return m;    
  398.     }
  399.     
  400.     protected QTPlayer player;
  401.     protected MoviePresenter md;
  402.     
  403.     protected ImagePresenter playRel;
  404.     protected ImagePresenter playPress;
  405.     protected ImagePresenter playPlaying;
  406.             
  407.     protected ImagePresenter stopRel;
  408.     protected ImagePresenter stopPress;
  409.     protected ImagePresenter stopDeactive;
  410.     
  411.     protected ImagePresenter rewRel;
  412.     protected ImagePresenter rewPress;
  413.     protected ImagePresenter rewDeactive;
  414.             
  415.     protected MovieButton playButton;
  416.     protected MovieButton stopButton;
  417.     protected MovieButton rewindButton;    
  418.  
  419.     protected QTMouseTargetController buttonController;
  420.     protected ButtonActivator buttonActivator;
  421. }
  422.